//游戏对象
(function (window, undefined) {
  //创建that指向this
  var that;
  //游戏对象构造函数
  function Game(map, obj) {
    // 判断用户传入数据
    obj = obj instanceof Object ? obj : {};
    //创建飞机、怪兽、子弹对象
    this.plane = new Plane(map);
    this.enemy = new Enemy(obj);
    this.bullet = new Bullet();
    //指向舞台
    this.map = map;
    //重置that指向
    that = this;
  }
  //重新赋值原型对象
  Game.prototype = {
    //指向构造函数
    constructor: Game,
    //游戏执行方法
    inIt: function () {
      //执行键盘监听方法
      keyDown();
      //初始化
      //绘制飞机
      this.plane.render(this.map);
      //初始化怪兽
      this.enemy.inIt();
      //绘制怪兽
      this.enemy.render(this.map);
      //绘制子弹（如果有）
      this.bullet.render(this.map);
      //执行run方法让游戏执行
      run();
    }
  }
  //游戏执行函数
  function run() {
    //创建定时器
    var timer = setInterval(function () {
      //执行删除怪兽、飞机、子弹
      that.enemy.remove(that.map);
      that.plane.remove(that.map);
      that.bullet.remove(that.map);
      //更新子弹和怪兽位置
      that.bullet.upDate();
      that.enemy.upData(that.map);
      //判断子弹是否击中怪兽
      //遍历怪兽怪兽
      for (let i = that.enemy.items.length - 1; i >= 0; i--) {
        //获取每个怪兽的定位坐标
        var enemyX = that.enemy.items[i].x * that.enemy.width + 30;
        var enemyY = that.enemy.items[i].y * that.enemy.height + 30;
        //遍历子弹，让每个子弹和每个怪兽比对
        //条件是子弹的left定位要在怪兽左右两个界面之间，且子弹的top值也要在怪兽上下边界之间
        for (let j = that.bullet.item.length - 1; j >= 0; j--) {
          var bulletX = that.bullet.item[j].x;
          var bulletY = that.bullet.item[j].y;
          if (bulletX >= enemyX && bulletX <= enemyX + 50 && bulletY >= enemyY && bulletY <= enemyY + 50) {
            //如果击中，调用怪兽和子弹的死亡函数，删除对应的子弹和怪兽
            that.enemy.die(i);
            that.bullet.die(j);
            //记分板加一分
            f.innerText = parseInt(f.innerText) + 1;
          }
        }
      }
      //绘制怪兽、飞机、子弹
      that.enemy.render(that.map);
      that.plane.render(that.map);
      that.bullet.render(that.map);
      //判断是否游戏结束
      //判断条件：当场上怪兽全部死亡或者怪兽移动到最低点
      if (that.enemy.items.length === 0 || (that.enemy.items[that.enemy.items.length - 1].y + 1) * 50 >= 500) {
        //删除定时器
        clearInterval(timer);
        //清空舞台怪兽、飞机和子弹
        that.plane.remove(that.map);
        that.bullet.remove(that.map);
        that.enemy.remove(that.map);
        //展示结束界面
        end.style.display = 'block';
      }
      //每50ms执行一次
    }, 50)
  }
  //监听键盘按下函数
  function keyDown() {
    document.onkeydown = function (e) {
      //根据e.keyCode判断按下的是那个按键
      // 左37、右39、空格32
      var code = e.keyCode;
      switch (code) {
        //按下左键，飞机左移
        case 37:
          that.plane.x = that.plane.x > 30 ? that.plane.x -= 5 : 30;
          break;
          //按下右键，飞机右移
        case 39:
          that.plane.x = that.plane.x < that.plane.maxX ? that.plane.x += 5 : that.plane.maxX;
          break;
        case 13:
          break;
          //按下空格发出子弹（给子弹对象添加一枚新子弹，定位根据飞机位置计算）
        case 32:
          that.bullet.item.push({
            x: that.plane.x + that.plane.width / 2,
            y: that.plane.y
          })
          break;
      }
    }
  }
  //把游戏对象放到window中
  window.Game = Game;
})(window, undefined)